<div id="searchPopup" class="search-popup hide1">
    <header class="header">
        <div id="searchForm" class="search-form">
            <div class="search-area">
                <i class="search-icon"></i>
                <input id="keyword" type="text" name="kw" class="search-input">
                <button id="clearSearchKeyword" type="button" class="search-reset"  title="清空输入内容"></button>
            </div>
            <a id="closeSearchPopup" href="javascript:;" class="search-cancel" role="button">取消</a>
        </div>
    </header>
    <div id="searchHotHistory" class="search-hot-history">
        <div id="searchPopularWords" class="search-popular loading-animation">
            <div class="search-title-bar">
                <h5 class="search-title">大家都在搜</h5>
            </div>
            <div class="search-tags">
            </div>
        </div>
        <div id="searchHistory" class="hide1">
            <div class="search-title-bar">
                <h5 class="search-title">搜索历史</h5>
                <div class="search-operate">
                    <a id="clearSearchHistory" href="javascript:;" class="search-operate-a" role="button">
                        <i class="ui-icon-delete search-delete-icon"></i>清空
                    </a>
                </div>
            </div>
            <div class="search-tags"></div>
        </div>
    </div>
    <div id="searchList" class="search-list">
        <ul class="search-list-ul">

        </ul>
    </div>
</div>
<script>
    //搜索弹出
    _$("#openSearchPopup").click(function(){
        _$("#searchPopup").show();
        _$("#keyword").focus();
    });
    _$("#closeSearchPopup").click(function(){
        _$("#searchPopup").hide();
    });
    _$.ajax({
        type:'post',
        dataType:'json',
        data:{},
        url:'/allSearch',
        success:function(json){
            var html="";
            if(json['code']==1){
                if(json['bookInfo'].length<1){
                    _$("#searchPopularWords .search-tags").html('<a href="javascript:;" class="btn-line-gray jsSearchLink nosearch">暂无热门搜索</a>');
                }else{
                    for(var val in json['bookInfo']){
                        html+='<a href="/search?kw='+json['bookInfo'][val]['bName']+'" class="btn-line-gray jsSearchLink" data-keyword="'+json['bookInfo'][val]['bName']+'">'+json['bookInfo'][val]['bName']+'</a>';
                    }
                    _$("#searchPopularWords .search-tags").html(html);
                }
            }else{
                tip(json['msg']);
            }
        }
    })
    _$("#clearSearchKeyword").click(function(){
        _$("#keyword").val("");
        _$("#searchList").hide();
        _$("#searchHotHistory").show();
        _$(this).hide();
    });
    function excludeSpecial(s) {
        // 去掉转义字符
        s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
        // 去掉特殊字符
        s = s.replace(/[\@\#\$\%\^\&\*\{\}\:\"\L\<\>\?]/);
        return s;
    };
    _$("#keyword").focus(function(){
        var kw=_$(this).val();
        if(kw.length>0){
            _$("#clearSearchKeyword,#searchList").show();
            _$("#searchHotHistory").hide();
            jQuery.ajax({
                type:'post',
                dataType:'json',
                url:'/getSearchBookKeyUp',
                data:{
                    kw:kw,
                    tag:1,
                    page:0
                },
                success:function(json){
                    console.log(json)
                    var html="";
                    var info=json['info']
                    if(json['code']==1){
                        for(var i=0;i<info.length;i++){
                            html+='<li class="search-list-li">';
                            html+='<i class="icon-search"></i>';
                            html+='<a href="/book/'+info[i]['bid']+'" class="search-list-a jsSearchLink">';
                            html+='<span class="search-list-l">'+info[i]['bName']+'</span>';
                            html+='</a>';
                            html+='</li>';
                        }
                        _$("#searchList ul").html(html);
                    }else{
                        tip("访问服务器出错");
                    }
                }
            })
        }
    })
    _$("#keyword").keyup(function(){
        var kw=_$(this).val();
        if(kw.length>0){
            _$("#clearSearchKeyword,#searchList").show();
            _$("#searchHotHistory").hide();
            _$.ajax({
                type:'post',
                dataType:'json',
                url:'/getSearchBookKeyUp',
                data:{
                    kw:kw,
                    tag:1,
                    page:0
                },
                success:function(json){
                    console.log(json)
                    var html="";
                    var info=json['info']
                    if(json['code']==1){
                        for(var i=0;i<info.length;i++){
                            html+='<li class="search-list-li">';
                            html+='<i class="icon-search"></i>';
                            html+='<a href="/book/'+info[i]['bid']+'" class="search-list-a jsSearchLink">';
                            html+='<span class="search-list-l">'+info[i]['bName']+'</span>';
                            html+='</a>';
                            html+='</li>';
                        }
                        _$("#searchList ul").html(html);
                    }else{
                        tip("访问服务器出错");
                    }
                }
            })
        }else{
            _$("#clearSearchKeyword").hide();
            _$("#searchList").hide();
            _$("#searchHotHistory").show();
        }
    });
    var sh=localStorage.getItem('SEARCH_HISTORY');
    if(sh){
        _$("#searchHistory").show();
        var html="";
        var shArr=JSON.parse(sh);
        for(var k=0;k<shArr.length;k++){
            html+='<a href="/search?kw='+shArr[k]['hisBooks']+'" class="btn-line-gray jsSearchLink" data-keyword="'+shArr[k]['hisBooks']+'">'+shArr[k]['hisBooks']+'</a>';
        }
        _$("#searchHistory .search-tags").html(html);
    }else{
        _$("#searchHistory").hide();
    }
    //搜索回车事件
    _$("#keyword").keydown(function(e){
        var kw=_$("#keyword").val();
        if(e.which==13){
            console.log(kw);
            if(kw==""){
                tip("请输入书名");
            }else{
                location.href='/search?kw='+kw;
            }
        }
    })
    _$("#clearSearchHistory").click(function(){
        localStorage.removeItem('SEARCH_HISTORY');
        _$("#searchHistory").hide();
        _$("#searchHistory .search-tags").html("");
    })
</script>